package sort;

public class sort {
    public static void main(String[] args) {
        int[] a = {3, 1, 4, 2, 8, 6, 7, 5};
        for (int i = 1; i < a.length; i++) {
            int temp = a[i];
            int j;
            for (j = i - 1; j >= 0; j--) {
                if (a[j] > temp) {
                    a[j + 1] = a[j];
                } else {
                    break;
                }
            }
            a[j + 1] = temp;
        }
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        System.out.println("****************************************");

        for (int i = 1; i < a.length; i++) {
            int temp = a[i];
            int low = 0;
            int high = i - 1;
            while (low <= high) {
                int mid = (low + high) / 2;
                if (temp > a[mid]) {
                    low = mid + 1;
                } else {
                    high = mid - 1;
                }
            }
            a[low] = temp;
        }
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        System.out.println("****************************************");

        int h = 1;
        while (h < a.length / 3) {
            h = 3 * h + 1;
        }
        while (h > 0) {
            for (int i = h; i < a.length; i++) {
                int temp = a[i];
                if (a[i] < a[i - h]) {
                    int j = i - h;
                    for (; j >= 0 && a[j] > temp; j -= h) {
                        a[j + h] = a[j];
                    }
                    a[j + h] = temp;
                }
            }
            h = (h - 1) / 3;
        }
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        System.out.println("****************************************");

        for (int i = 0; i < a.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[i]) {
                    min = j;
                }
            }
            if (min != i) {
                int temp = a[i];
                a[i] = a[min];
                a[min] = temp;
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        System.out.println("****************************************");

        for (int i = 0; i < a.length - 1; i++) {
            boolean bln = true;
            for (int j = 0; j < a.length - i - 1; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                    bln = false;
                }
            }
            if (bln) {
                break;
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}
